package com.zhdl.modules.prefiresys.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.IdUtil;
import com.project.common.service.impl.BaseServiceImpl;
import com.zhdl.modules.prefiresys.entity.DefAreaHourRec;
import com.zhdl.modules.prefiresys.entity.QDefAreaHourRec;
import com.zhdl.modules.prefiresys.repository.DefAreaHourRecRepository;
import com.zhdl.modules.prefiresys.service.DefAreaHourRecService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 防区小时记录
 */
@Slf4j
@Service
public class DefAreaHourRecServiceImpl extends BaseServiceImpl<String, DefAreaHourRec, DefAreaHourRecRepository> implements DefAreaHourRecService {
    /**
     * 查询防区 记录信息
     *
     * @param areaId 防区Id
     * @return
     */
    @Override
    public List<DefAreaHourRec> findByAreaId(String areaId) {
        // 基本查询
        LocalDate cur = LocalDate.now();    // 获取当前日期
        QDefAreaHourRec qe = QDefAreaHourRec.defAreaHourRec;
        return jpaQueryFactory().selectFrom(qe)
                .where(
                        qe.areaId.eq(areaId),
                        qe.createDate.gt(cur.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")))
                )
                .orderBy(qe.hour.asc())
                .fetch();
    }

    /**
     * 保存防区小时浓度
     *
     * @param deviceId         数据库Id
     * @param areaId           防区Id
     * @param particlePercent1 微粒子浓度百分比
     * @param smokePercent1    烟雾浓度百分比
     * @return
     */
    @Override
    @Transactional
    public long addData(String deviceId, String areaId, Float particlePercent1, Float smokePercent1) {
        // 删除昨天产生的历史数据
        DateTime dateTime = DateTime.now();
        Integer curHour = dateTime.hour(true);

        QDefAreaHourRec qe = QDefAreaHourRec.defAreaHourRec;
        int count = jpaQueryFactory().selectFrom(qe)
                .where(
                        qe.areaId.eq(areaId),
                        qe.hour.eq(curHour),
                        qe.createDate.gt(dateTime.toString("yyyy-MM-dd"))
                )
                .orderBy(qe.hour.asc())
                .fetch()
                .size();

        if (count > 0) return 99;

        // 不存在则添加
        DefAreaHourRec defAreaHourRec = new DefAreaHourRec();
        defAreaHourRec.setId(IdUtil.fastSimpleUUID());
        defAreaHourRec.setDetectorId(deviceId);
        defAreaHourRec.setAreaId(areaId);
        defAreaHourRec.setHour(curHour);
        defAreaHourRec.setParticlePercent(particlePercent1);
        defAreaHourRec.setSmokePercent(smokePercent1);
        defAreaHourRec.setReportStatus(0);//上报标志初始为0
        insert(defAreaHourRec);

        // 增加当前小时防区浓度数据
        long dbRes = jpaQueryFactory()
                .delete(qe)
                .where(qe.createDate.lt(dateTime.toString("yyyy-MM-dd")))
                .execute();
        if (dbRes == 0) return -1;

        return dbRes;
    }
}
